﻿

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Design;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
using FGame = Freeform.Game;
using XGame = Microsoft.Xna.Framework.Game;
using FComponent = Freeform.Component;

#endregion


namespace Freeform
{
    /// <summary>
    /// A class that renders an entity as a 2D sprite.
    /// </summary>
    public class Actor2D_Image : Actor2D
    {

        #region Members

        /// <summary>
        /// Gets a reference to the animation set for this actor.
        /// </summary>
        public AnimationSet2D Animations { get; private set; }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new 2d image actor.
        /// </summary>
        /// <param name="Entity">The entity that this actor represents.</param>
        public Actor2D_Image(Entity2D Entity)
            : base(Entity)
        {
            Animations = new AnimationSet2D();
        }


        /// <summary>
        /// Creates a new 2d image actor.
        /// </summary>
        /// <param name="Entity">The entity that this actor represents.</param>
        /// <param name="Animations">The animation set for the actor.</param>
        public Actor2D_Image(Entity2D Entity, AnimationSet2D Animations)
            : base(Entity)
        {
            this.Animations = Animations;
        }

        #endregion


        #region Draw

        /// <summary>
        /// Draws the entity onto the viewport.
        /// </summary>
        /// <param name="Viewport">The viewport onto which the entity is being rendered.</param>
        /// <param name="Time">Provides a snapshot of timing values.</param>
        public override void Draw(Viewport2D Viewport, GameTime Time)
        {
            // Check early breaks.
            if (Viewport.SpriteBatch == null) return;
            if (Animations.Current == null) return;
            if (Entity == null) return;

            // Calculate drawing data.
            Animation2D Draw_Animation = Animations.Current;
            Vector2 Draw_Position = Viewport.Camera.TransformPosition(Entity.Position);
            Vector2 Draw_Size = Viewport.Camera.TransformSize(Entity.Size);
            Vector2 Draw_Origin = new Vector2(Draw_Animation.WidthPerCell / 2, Draw_Animation.HeightPerCell / 2);
            Rectangle Draw_Destination = new Rectangle((int)Draw_Position.X, (int)Draw_Position.Y, (int)Draw_Size.X, (int)Draw_Size.Y);
            Rectangle Draw_Source = Draw_Animation.UpdateSource(Time);

            // Draw entity.
            Viewport.SpriteBatch.Draw(Draw_Animation.Texture, Draw_Destination, Draw_Source, ColourOverlay, Entity.Orientation, Draw_Origin, SpriteEffects.None, Depth);
        }

        #endregion

    }
}
